#include "include/HttpServer.h"
#include "include/HttpService.h"
#include "include/hlog.h"

using namespace hv;

int main() {
    // 创建HTTP服务并配置路由
    HttpService router;
    router.POST("/echo", [](HttpRequest* req, HttpResponse* resp) {
        // 读取请求体（并未处理Get请求）
        resp->SetBody("Get From Client: " + req->body);
        return 200;
    });

    // 创建HTTP服务器
    HttpServer server(&router);
    server.https_port = 8080;
    // 配置SSL/TLS
    hssl_ctx_init_param_t param;
    memset(&param, 0, sizeof(param));
    
    // 服务器证书和私钥
    param.crt_file = "../cert/server.crt";
    param.key_file = "../cert/server_rsa_private.pem";
    // CA证书用于验证客户端证书
    param.ca_file = "../cert/ca.crt";
    // 启用客户端证书验证
    param.verify_peer = 1;
    param.endpoint = HSSL_SERVER;

    // 初始化SSL上下文
    // if (hssl_ctx_init(&param) != 0) {
    //     std::cout << "failed to init ssl context\n";
    //     // hloge("Failed to initialize SSL context!");
    //     return -1;
    // }
    if(server.newSslCtx(&param) != 0) {
        std::cout << "failed to init ssl context\n";
        return -1;
    }
    // 启动HTTPS服务器
    // hlogi("Starting HTTPS server on port 8080...");
    std::cout << "Starting HTTPS server on port 8080...\n";
    server.run();

    return 0;
}
